<?php
namespace Index\Controller;
use Think\Controller;
class JieController extends CommonController {
    public function index(){
    //发布问题
    	$id=I('get.id');      
      $uid=isset($_SESSION['uid'])?$_SESSION['uid']:'';//(三元表达式)

                                              //(括号里面的这句是下面collect方法的)is_coll
    	$sql = "select t1.*,t2.face,t2.nickname,(select uid from collect where qid='$id' and uid='$uid') is_coll  from question t1 left join user t2 on t1.uid=t2.id where t1.id='$id' ";
      $this->assign('id',$id);

    	$data=M()->query($sql);

      M('question')->where("id='$id'")->setInc('view_num',1);  

      //查回答表和用户表
      $sql="select t1.*,t2.face,t2.nickname,(select uid from zan where uid='$uid' and aid=t1.id) is_zan from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='$id' order by t1.status desc";
      $arranwer=M()->query($sql);
      // var_dump($arranwer);
      $this->assign('arranwer',$arranwer);
      // var_dump($arranwer);
    	$hotview=D('question')->hotview();

    	$hotanswer=D('question')->hotanswer();

    	$this->assign('hotview',$hotview);
    	// var_dump($hotanswer);
    	$this->assign('hotanswer',$hotanswer);

    	$this->assign('ques',current($data));
      $this->assign('title',current($data)['title']);
       $this->display();
   }
   //点发布跳转到这方法
   	public function add()
   	{
      //公共控制器里个方法
      // $this->is_login();

   		$cate = M('category')->select();
   		$tempArr=[];
   		foreach ($cate as $row) {
   			if ($row['fid']==0) {
   				$tempArr[$row['id']]=$row;
   			}
   		}
   		foreach ($cate as $son) {
   			foreach ($tempArr as $key => $fater) {
   				if ($son['fid']==$key) {
   					$tempArr[$key]['son'][]=$son;
   				}
   			}
   		}
   		$this->assign('cate',$tempArr);
   		//随机获取一条问题
    	$question = D('vercode')->getOne();
        //分配变量
    	$this->assign('question',$question);

   		$this->display();
   	}
   	//异步传进这方法
   	public function addjie()
   	{
   			D('vercode')->checkCode(I('post.vercode'));

   			$data = I('post.');
	    	$data['uid']= $_SESSION['uid'];
	    	$data['create_time']=time();
	    	$idd=$_SESSION['uid'];
	    	//$id是文章id  $idd是用户id
   			if ($id=M('question')->add($data)) {

   				M('user')->where("id='$idd'")->setDec('kiss',$data['kiss']);
   											//setDec用户的积分减$data['kiss']
   				$url=U('index/jie/index',array('id'=>$id));
   				
   				Reajax(0,array('info'=>"发布成功",'url'=>$url));
   			}else{
   				Reajax(1,"发布失败");
   			}
   	}
    //发布图片上传
   	public function upload()
   	{
   				$upload = new \Think\Upload();// 实例化上传类
			    $upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			    $upload->rootPath  =     './Uploads/jie/'; // 设置附件上传根目录
			    $upload->savePath  =     ''; // 设置附件上传（子）目录
			    // 上传文件 
			    $info   =   $upload->upload();
			   
			    if(!$info) {// 上传错误提示错误信息
			        $this->error($upload->getError());
			    }else{// 上传成功
			       
			    	$info=current($info);

			        $img=__ROOT__."/Uploads/jie/".$info['savepath'].$info['savename'];
			        $arr=array(
			        	'code'=>0,
			        	'msg'=>"成功",
			        	'data'=>array(
			        		'src'=>$img,
			       		 )
	        		);
	        		echo json_encode($arr);
	    		}
   	}
    //全部展现页面
    public function jie()
    {
       //取置顶   
        $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.top=1 limit 10";
        $topData=M()->query($sql);

        $count      = M('question')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        //取主页展现的
        $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id order by t1.create_time desc limit ".$Page->firstRow.','.$Page->listRows;

        $arrData=M()->query($sql);

        //分配
        $this->assign('show',$show);
        $this->assign('arrData',$arrData);
        $this->assign('topData',$topData);
      $this->display();
    }
    //未结贴站着页面
    public function unsolved()
    {
        //取置顶   
        $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.top=1 limit 20";
        $topData=M()->query($sql);


        $count      = M('question')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        //取主页展现的
        $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id  where  t1.status='未结贴'  order by t1.create_time desc limit ".$Page->firstRow.','.$Page->listRows;
        $arrData=M()->query($sql);
        //分配
        $this->assign('show',$show);
        $this->assign('arrData',$arrData);
        $this->assign('topData',$topData);
         $this->display();
    }
    //已采纳展现页面
    public function solved()
    {
      $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status='已采纳' limit 20";
      $solved=M()->query($sql);
      // var_dump($solved);
      $this->assign('solved',$solved);
      $this->display();
      // var_dump(999999);
    }
    //加精贴展现页面
    public function wonderful()
    {   //三张表去出精贴药用的信息
      $sql="select t1.*,t2.face,t2.nickname,t3.name from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.jing=1 limit 20";
        $jingData=M()->query($sql);
         // var_dump($jingData);
        $this->assign('jingData',$jingData);
        $this->display();
       
    }
    //回答时收到的
    public function answer()
    {
      $id=$_SESSION['uid'];
      $data=I('post.');
      $data['create_time']=time();
      $data['uid']=$id;
      // var_dump($data['qid']);
      $qid=$data['qid'];
      
      //品论+1
      // M('question')->where("id='$data['qid']'")->setInc('answer_num',1);
      M('question')->where("id='$qid'")->setInc('answer_num',1);
      //把提交内容存到回答表里
      if (M('answer')->add($data)) {
        Reajax(0,"成功回复");
      }else{
        Reajax(1,"不能回复");
      }
      
    }
    //点赞的方法
    public function jiezan()
    {
        $aid=$data['aid']=I('post.aid');

        $uid=$data['uid']=$_SESSION['uid'];
        $data['create_time']=time();


        $zan=M('zan')->where("uid='$uid' and aid='$aid'")->find();
        if ($zan) {

          M('zan')->where("uid='$uid' and aid='$aid'")->delete();

          M('answer')->where("id='$aid'")->setDec('zan_num');

          Reajax(1,"点赞消失");

          }else{

            M('zan')->add($data);

            M('answer')->where("id='$aid'")->setInc('zan_num');

            Reajax(0,"点赞");
          }
    }

    //收藏方法
    public function collect()
    {
        $qid=I('post.qid');
        $uid=$_SESSION['uid'];
        


        $res=M('collect')->where("uid='$uid' and qid='$qid'")->find();
        if ($res) {

          M('collect')->where("uid='$uid' and qid='$qid'")->delete();
          Reajax(1,"消失收藏");

          }else{
            $data['uid']=$uid;
            $data['qid']=$qid;
            $data['create_time']=time();
            M('collect')->add($data);

            Reajax(0,"已收藏");
          }
    }
    //点击采纳
    public function status()
    {
       $data=I('post.');//回答id  问题id

       $qid=$data['qid'];//问题id
       $aid=$data['aid'];//回答id
  
       $uid=$_SESSION['uid'];//采纳用户id

      $sql="select * from answer where id='$aid'";
      $aaa=M()->query($sql);
      // var_dump($aaa);
      $an_id=current($aaa)['uid'];//回答表uid
      //判断不能自己采纳自己
      if ($uid==$an_id) {
        Reajax(1,"不能采纳自己的回答");
      }

      $data1['status']='1';

      $num['status']='1';

      $res = M('answer')->where("id='$aid'")->save($data1);//更改回答表状态为'1'
      
      
      $res1 = M('question')->where("id='$qid'")->save($num);//更改回答表状态为'已采纳'
      if ($res and $res1) {

        $sql="select kiss from question where id='$qid'";
        $kiss = M()->query($sql);
        $kiss = current($kiss)['kiss'];
      
        $res = M('user')->where("id='$an_id'")->setInc('kiss',$kiss);//添加飞吻

        Reajax(0,"采纳成功");
      }else{
        Reajax(1,"采纳不成功");
      }
    }







}

?>